# -*- coding: utf-8 -*-

from contextlib import asynccontextmanager
from typing import Union
from fastapi import FastAPI

from config.log_config import LOG_CONFIIG
from router.task import task_router

import logging

from router.websocket import ws_api

logger = logging.getLogger(__name__)


@asynccontextmanager
async def lifespan(api: FastAPI):
    logger.info('startup')
    yield
    logger.info('shutdown')


app = FastAPI(lifespan=lifespan)
app.include_router(task_router)
app.mount('/ws', ws_api)


@app.get('/')
def index():
    return {'Hello': 'World'}


@app.get('/get/{path}')
def get(path: str, query: Union[str, None] = None):
    """
    GET请求路径参数和请求参数
    :param path:
    :param query:
    :return:
    """
    return {'path': path, 'query': query}


# @app.on_event已弃用，使用lifespan替代
# @app.on_event(event_type='startup')
# def startup():
#     pass
#
#
# @app.on_event(event_type='shutdown')
# def shutdown():
#     pass

if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, host='0.0.0.0', port=8000, log_config=LOG_CONFIIG)
    pass
